package cn.com.yiqi.wsc.service.balance.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.balance.BalanceFetchDao;
import cn.com.yiqi.wsc.dao.balance.criteria.BalanceCriteria;
import cn.com.yiqi.wsc.dao.balance.entity.BalanceFetchEntityDTO;
import cn.com.yiqi.wsc.entity.check.Check;
import cn.com.yiqi.wsc.enumeration.CheckResultEnum;
import cn.com.yiqi.wsc.enumeration.CheckTypeEnum;
import cn.com.yiqi.wsc.service.balance.BalanceService;
import cn.com.yiqi.wsc.service.balance.PortalBalanceService;
import cn.com.yiqi.wsc.service.check.CheckService;
import cn.com.yiqi.wsc.service.point.PortalPointService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 何刚
 * @version V1.0
 * @Title: PortalBalanceServiceImpl
 * @Package cn.com.yiqi.wsc.service.balance.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/3/21
 */
@Service
public class PortalBalanceServiceImpl implements PortalBalanceService {


    @Autowired
    private BalanceFetchDao balanceFetchDao;

    @Autowired
    private PortalPointService portalPointService;

    @Autowired
    private BalanceService balanceService;

    @Autowired
    private CheckService checkService;


    @Override
    public Result<PageInfo<BalanceFetchEntityDTO>> pageList(BalanceCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Date end = criteria.getApplyTimeEnd();
        if (end != null) {
            end = DateUtils.addDays(end, 1);
            criteria.setApplyTimeEnd(end);
        }

        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());
        PageInfo<BalanceFetchEntityDTO> pageInfo = new PageInfo<>(balanceFetchDao.pageList(criteria));

        result.setReturnValue(pageInfo);

        return result;
    }

    @Transactional
    @Override
    public Result check(Check check) {

        Result result = new Result<>();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        check.setContentType(CheckTypeEnum.BALANCE_FETCH.getValue());

        Long fetchId = check.getContentId().longValue();

        if (CheckResultEnum.ACCESS.getValue() == check.getCheckResult()) {
            //通过
            Result fetchResult = balanceService.passFetchRequest(fetchId);
            if (!ResultUtil.isSuccess(fetchResult)) {
                return ResultUtil.paramError(result, fetchResult.getErrMsg());
            }
        } else {

            //不通过
            balanceService.notPassFetchRequest(fetchId);

        }

        //修改检查记录
        checkService.insertCheck(check);

        return result;

    }
}
